package pl.elka.pszt.model;

import java.util.HashSet;
import java.util.Set;

import pl.elka.pszt.model.literal.Literal;

/**
 * Klauzula koniunkcyjno-implikacyjna ktora sklada sie ze zbioru literalow
 * bedacych w koniunkcji oraz literalu bedacego konkluzja.
 * 
 * @author Andrzej Makarewicz
 * 
 */
public class Clause extends KnowledgeElement {
	
	final Set<Literal> conjunction;
	
	public Clause(Literal implication) {
		this(new HashSet<Literal>(), implication);
	}
	
	public Clause(Set<Literal> conjunction, Literal implication) {
		super(implication, Type.CLAUSE);
		
		if (conjunction == null) throw new NullPointerException();
		this.conjunction = conjunction;
	}
	
	/**
	 * 
	 * @return zbior literalow koniunkcji
	 */
	public Set<Literal> getConjunction() {
		return conjunction;
	}
	
	public boolean addLiteralToConjunction(Literal literal) {
		return conjunction.add(literal);
	}
	
	public boolean addLiteralsToConjunction(Set<Literal> literals) {
		return conjunction.addAll(literals);
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (Literal literal : conjunction) {
			sb.append(literal.toString());
			sb.append(" ^ ");
		}
		sb.replace(sb.length() - 3, sb.length(), "");
		
		sb.append(" => ");
		sb.append(conclusion.toString());
		
		return sb.toString();
	}
	
}
